package com.example.springbook.Service;


import com.example.springbook.Model.BookInfo;
import com.example.springbook.Model.PageRequest;
import com.example.springbook.Model.PageResult;
import com.example.springbook.enums.BookStatus;
import com.example.springbook.mapper.BookInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
@Component //交给spring进行管理
public class BookService {

    @Autowired
    private BookInfoMapper bookInfoMapper;


    public Integer insertBook(BookInfo bookInfo){
       return bookInfoMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
        List<BookInfo> bookInfo = bookInfoMapper.getBookListByPage(pageRequest.getOffest(), pageRequest.pageSize);
        Integer count=bookInfoMapper.getBookCount();

        for (BookInfo book : bookInfo) {
           book.setStatusCN(BookStatus.getStatus(book.getStatus()).getDesc());
        }

        return new PageResult<>(bookInfo,count,pageRequest);
    }


    public BookInfo queryBookById(Integer id) {
        BookInfo bookInfo=bookInfoMapper.queryBookById(id);
        bookInfo.setStatusCN(BookStatus.getStatus(bookInfo.getStatus()).getDesc());
        return bookInfo;
    }

    public void updateBook(BookInfo bookInfo) {
        bookInfoMapper.updateBook(bookInfo);
    }

    public Integer deleteBook(Integer bookId) {

        return bookInfoMapper.deleteBook(bookId);
    }

    public Integer batchDeleteBook(List<Integer> ids) {
        return bookInfoMapper.batchDeleteBook(ids);
    }
}
